 /*
  * 统一日志模块：集中环境判断与日志级别控制
  * - 开发环境：输出所有级别
  * - 生产环境：默认仅输出 warn/error，可按需调整
  */
 
 type LogLevel = 'debug' | 'log' | 'info' | 'warn' | 'error'
 
 interface Logger {
   debug: (...args: unknown[]) => void
   log: (...args: unknown[]) => void
   info: (...args: unknown[]) => void
   warn: (...args: unknown[]) => void
   error: (...args: unknown[]) => void
 }
 
const isProduction = import.meta.env.PROD

// 生产环境启用的级别（可按需调整）
const enabledInProduction: Set<LogLevel> = new Set(['warn', 'error'])

// 打印前缀，便于快速定位来源
function formatPrefix(level: LogLevel, scope?: string): string[] {
   const time = new Date().toISOString()
   const tag = scope ? `[${scope}]` : ''
   return [`[${level.toUpperCase()}]`, time, tag].filter(Boolean)
 }
 
 function shouldLog(level: LogLevel): boolean {
   if (!isProduction) return true
   return enabledInProduction.has(level)
 }
 
// 绑定原生 console 方法，避免后续覆盖导致递归
const original = {
  debug: typeof globalThis.console.debug === 'function' ? globalThis.console.debug.bind(globalThis.console) : undefined,
  log: globalThis.console.log.bind(globalThis.console),
  info: typeof globalThis.console.info === 'function' ? globalThis.console.info.bind(globalThis.console) : globalThis.console.log.bind(globalThis.console),
  warn: globalThis.console.warn.bind(globalThis.console),
  error: globalThis.console.error.bind(globalThis.console),
}

function createPrinter(level: LogLevel, scope?: string) {
   return (...args: unknown[]) => {
     if (!shouldLog(level)) return
     const parts = [...formatPrefix(level, scope), ...args]
     switch (level) {
       case 'debug':
        original.debug?.(...parts)
         break
       case 'log':
        original.log(...parts)
         break
       case 'info':
        original.info?.(...parts) ?? original.log(...parts)
         break
       case 'warn':
        original.warn(...parts)
         break
       case 'error':
        original.error(...parts)
         break
     }
   }
 }
 
 export const logger: Logger = {
  debug: createPrinter('debug'),
  log: createPrinter('log'),
  info: createPrinter('info'),
  warn: createPrinter('warn'),
  error: createPrinter('error'),
 }
 
 export function createScopedLogger(scope: string): Logger {
   return {
    debug: createPrinter('debug', scope),
    log: createPrinter('log', scope),
    info: createPrinter('info', scope),
    warn: createPrinter('warn', scope),
    error: createPrinter('error', scope),
   }
 }
 
 // 覆盖全局 console，使现有 console.* 调用走统一策略
 export function installConsoleInterceptor(): void {
  const markerKey = '__APP_LOGGER_INSTALLED__'
  const anyConsole = globalThis.console as unknown as Record<string, unknown>
  if (anyConsole[markerKey]) return

  const scoped = createScopedLogger('app')

  globalThis.console.debug = scoped.debug
  globalThis.console.log = scoped.log
  globalThis.console.info = scoped.info
  globalThis.console.warn = scoped.warn
  globalThis.console.error = scoped.error

  // 标记，避免重复安装（HMR/多次执行）
  anyConsole[markerKey] = true
 }
 
 export default logger
 
 
